package work

import (
	"goskeleton/app/global/consts"
	// "goskeleton/app/global/variable"
	// "go.uber.org/zap"
	"goskeleton/app/service/work/curd"
	"goskeleton/app/utils/response"

	"github.com/gin-gonic/gin"
)

type Work struct {
}

func (w *Work) List(context *gin.Context) {
	page := context.GetFloat64(consts.ValidatorPrefix + "page")
	limit := context.GetFloat64(consts.ValidatorPrefix + "limit")

	settlementStatus := context.GetFloat64(consts.ValidatorPrefix + "settlement_status")
	settlementMethod := context.GetFloat64(consts.ValidatorPrefix + "settlement_method")
	paymentMethod := context.GetFloat64(consts.ValidatorPrefix + "payment_method")
	invoiceStatus := context.GetFloat64(consts.ValidatorPrefix + "invoice_status")
	settlementDateStart := context.GetString(consts.ValidatorPrefix + "settlement_date_start")
	settlementDateEnd := context.GetString(consts.ValidatorPrefix + "settlement_date_end")
	invoiceDateStart := context.GetString(consts.ValidatorPrefix + "invoice_date_start")
	invoiceDateEnd := context.GetString(consts.ValidatorPrefix + "invoice_date_end")
	paymentDateStart := context.GetString(consts.ValidatorPrefix + "payment_date_start")
	paymentDateEnd := context.GetString(consts.ValidatorPrefix + "payment_date_end")

	// 正确获取 department_employee_ids，它是一个 []int 类型的切片
	var departmentEmployeeIds []int
	if ids, exists := context.Get("department_employee_ids"); exists {
		if idSlice, ok := ids.([]int); ok {
			departmentEmployeeIds = idSlice
		}
	}
	
	orderBy := "id asc"
	limitStart := (page - 1) * limit

	totalCounts, showList := curd.
		CreateWorkCurdFactory().
		ListData(
			int(limitStart),
			int(limit),
			orderBy,
			int(settlementStatus),
			int(settlementMethod),
			int(paymentMethod),
			int(invoiceStatus),
			settlementDateStart,
			settlementDateEnd,
			invoiceDateStart,
			invoiceDateEnd,
			paymentDateStart,
			paymentDateEnd,
			departmentEmployeeIds, // 传递正确的 []int 类型参数
		)

	if totalCounts > 0 && showList != nil {
		response.Success(context, consts.CurdStatusOkMsg, gin.H{"count": totalCounts, "data": showList})
	} else {
		response.Fail(context, consts.CurdSelectFailCode, consts.CurdSelectFailMsg, "")
	}
}

func (w *Work) Detail(context *gin.Context) {
	customerId := context.GetFloat64(consts.ValidatorPrefix + "id")
	detail := curd.CreateWorkCurdFactory().ShowOneItem(int(customerId))

	if detail == nil {

		response.Fail(context, consts.CurdSelectFailCode, consts.CurdSelectFailMsg, "")
	} else {
		response.Success(context, consts.CurdStatusOkMsg, detail)
	}
}

func (w *Work) Create(context *gin.Context) {
	if curd.CreateWorkCurdFactory().InsertData(context) {
		response.Success(context, consts.CurdStatusOkMsg, "")
	} else {
		response.Fail(context, consts.CurdCreatFailCode, consts.CurdCreatFailMsg, "")
	}
}

func (w *Work) Edit(context *gin.Context) {
	if curd.CreateWorkCurdFactory().UpdateData(context) {
		response.Success(context, consts.CurdStatusOkMsg, "")
	} else {
		response.Fail(context, consts.CurdUpdateFailCode, consts.CurdUpdateFailMsg, "")
	}
}

func (w *Work) TotalStatistics(context *gin.Context) {
	totalStatusStatistics := curd.CreateWorkCurdFactory().TotalStatistics()
	if totalStatusStatistics == nil {
		response.Fail(context, consts.CurdSelectFailCode, consts.CurdSelectFailMsg, "")
	} else {
		response.Success(context, consts.CurdStatusOkMsg, totalStatusStatistics)
	}
}

func (w *Work) Delete(context *gin.Context) {
	workId := context.GetFloat64(consts.ValidatorPrefix + "id")
	if curd.CreateWorkCurdFactory().DeleteData(int(workId)) {
		response.Success(context, consts.CurdStatusOkMsg, "")
	} else {
		response.Fail(context, consts.CurdDeleteFailCode, consts.CurdDeleteFailMsg, "")
	}
}
